<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MembersController
 *
 * @author Zoila
 */
class MembersController extends AppController {
    public $uses = array('Member','User','OrgMember','Team','Request');
    
    //put your code here
     public function isAuthorized($user) {
        if (in_array($this->action, array('view', 'add'))) {
            return true;
        }

        // The owner of a post can edit and delete it
        if (in_array($this->action, array('edit', 'delete'))) {
            $id = $this->request->params['pass'][0];
//            if ($this->Member->isOwnedBy($id, $user['id'])) {
//                return true;
//            }
            return true; //mientras
        }
        return parent::isAuthorized($user);
    }
    
     public function add( $idTeam, $ind =null){
        
        
        if (($idTeam!=null)&&($this->request->data['Member']['user_id']!=null)) {
            if ($this->request->is('post')){  
                $id=$this->request->data['Member']['user_id'];
                $this->request->data['Member']['creator_id'] = $this->Auth->user('id');
                $this->request->data['Member']['team_id'] = $idTeam;
                $user=$this->User->findById($id) ;
                $team=$this->Team->findById($idTeam) ;
                $this->request->data['Member']['name'] = $user['User']['username'];
                if ($this->request->data['Member']['user_id']==$team['Team']['creator_id']){
                    $this->request->data['Member']['state']=1; //CREADOR
                }
                else {
                    $this->request->data['Member']['state']=0;
                    //ENVIAR INVITACION;
                    $rq=array('user_from'=>$this->Auth->user('id'),
                        'user_to'=>$id,'team_id'=>$idTeam,'type'=>1,'accepted'=>0);
                    $this->Request->save($rq);
                }
                if($this->Member->save($this->request->data)){
                    
                    //HAY QUE AGREGAR EL MIEMBRO A LA ORG, SI ES Q NO EXISTE...
                    $or=$this->Session->read('Organization'); 
                    $orgMember= $this->OrgMember->findByOrganizationIdAndUserId($or['Organization']['id'],$this->request->data['Member']['user_id']);
                    if (is_null($orgMember['OrgMember'])|| (count($orgMember)==0)){
                        $orgMember= array('organization_id'=>$or['Organization']['id'],
                            'user_id'=> $this->request->data['Member']['user_id'],
                            'state' => 0);
                        $this->OrgMember->save($orgMember);
                    }
                    
                    $this->Session->setFlash($orgMember['OrgMember']['user_id'].'The member has been added.');
                    if ($ind == 1) //antes estuvo en teams/view/
                        $this->redirect(array('controller'=>'teams','action'=>'view',$idTeam));
                     var_dump($orgMember);
                }
                else{
                    $this->Session-> setFlash('Unable to add the member');
                    if ($ind == 1) //antes estuvo en teams/view/
                        $this->redirect(array('controller'=>'teams','action'=>'view',$idTeam));
                }
            }
        }
        else {
            $this->Session->setFlash('Elija un usuario');
            if ($ind == 1)
                 $this->redirect(array('controller'=>'teams','action'=>'view',$idTeam));
            
           // $this->set('error',true);
        }
        
        
        
    }
    
     public function delete($idUser,$idTeam) {
        
        if ($this->request->is('get')) {
            throw new MethodNotAllowedException();
        }
        $this->Member->query('DELETE FROM members where user_id='.$idUser.' and team_id='.$idTeam);
              //  $this->Member->deleteAll(array('Member.user_id' => $idUser,'Member.team_id' => $idTeam))) {
            $this->Session->setFlash('The member with id: ' . $idUser . ' has been deleted.');
            $this->redirect(array('controller'=>'teams','action'=>'view',$idTeam));
        
        
    }
    
    
}

?>
